package com.ilovepostcard;

import java.util.Date;

import com.ilovepostcard.controller.PostcardController;
import com.ilovepostcard.controller.ShareController;
import com.ilovepostcard.data.Postcard;
import com.ilovepostcard.data.SendTaskInfo;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.utils.Utils;
import com.mobclick.android.MobclickAgent;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

public class PrintChangeTypeActivity extends BaseActivity implements OnClickListener{

	ImageView mReturn;
	Button mConfirm;
	RadioButton single, multi;
	boolean isNewPostCard;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_print_choosetype);
		
		Intent intent = getIntent();
		if(intent != null)
		{
			Bundle bundle = intent.getExtras();
			isNewPostCard = true;
			if(bundle != null)
			{
				com.ilovepostcard.model.Postcard pc = (com.ilovepostcard.model.Postcard)bundle.getSerializable("postcard");
				if(pc!= null)
				{
					Postcard sharePc = new Postcard();
					sharePc.id =pc.PostcardID;
					sharePc.comment = pc.ToFriendWords;
					sharePc.latitude = pc.Latitude;
					sharePc.longitude = pc.Longitude;
					sharePc.canBeFoundFlag = pc.ViewPermission;
					sharePc.showLocationFlag = pc.LocationPermission;
					sharePc.location = pc.Location;
					sharePc.createTime = pc.PublishTime;
					sharePc.tags = new String[]{pc.TagIn};
					//sharePc.image = Utils.readBitmapByFile(pc.imagePath, 1);
					sharePc.setTmpEditPC(Utils.readBitmapByFile(pc.imagePath, 1));
					PostcardController.getInstance().createNewPostcard(sharePc);
					isNewPostCard = false;
				}
			}
		}
		ShareController.getInstance().createSendTask(new SendTaskInfo());
		init();
	}
	public void onResume()
	{
		super.onResume();
		MobclickAgent.onResume(this); 
	}

	protected void onPause()
	{
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	private void init()
	{
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(this);
		
		
		mConfirm = (Button)findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		
		single = (RadioButton)findViewById(R.id.single);
		single.setChecked(true);
		multi = (RadioButton)findViewById(R.id.multi);
	}
	
	public void onBackPressed() 
	{
		if(!isNewPostCard)
		{
			Intent intent = new Intent(this,MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return;
		}
		
		super.onBackPressed();
		
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		ShareController.getInstance().cancelCurrentSendTask();
	}


	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.title_return)
		{
			finish();
		}
		else if(v.getId() == R.id.confirm)
		{
			SendTaskInfo sendtask =  ShareController.getInstance().getCurrentSendTask();
			Intent nextPage= null;
			if(single.isChecked())
			{
				MobclickAgent.onEvent(this, Constants.COUNT_CREAT_SINGLE);
				sendtask.postcardList.clear();
				sendtask.postcardList.add(PostcardController.getInstance().getCurrentPostCard());
				sendtask.isSingle = true;
				nextPage = new Intent(PrintChangeTypeActivity.this,PrintConfirmCommentActivity.class);
			}
			else if(multi.isChecked())
			{
				MobclickAgent.onEvent(this, Constants.COUNT_CREAT_SUITE);
				sendtask.isSingle = false;
				nextPage = new Intent(PrintChangeTypeActivity.this,PrintAddPostcardActivity.class);
			}
			else
			{
				Toast.makeText(this, "请选择产品类型", Toast.LENGTH_SHORT).show();
			}
			
			if(nextPage!= null)
				PrintChangeTypeActivity.this.startActivity(nextPage);
		}
	}
}
